package com.controller;

import com.entity.ResponseMsg;
import com.entity.query.QueryOrder;
import com.entity.query.QueryResult;
import com.service.impl.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by bruce on 2016/5/22.
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    // 查询订单详情
    @RequestMapping(value = "list", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getOrderList(HttpServletRequest request) {

        Integer page, pageSize, status, storeId;

        QueryOrder queryOrder = new QueryOrder();
        List<Integer> orderState = new ArrayList<Integer>();

        String sortBy = request.getParameter("sortBy");
        String sortMethod = request.getParameter("sortMethod");

        storeId = NumberUtils.toInt(request.getParameter("storeId"));
        status = NumberUtils.toInt(request.getParameter("status"));
        page = NumberUtils.toInt(request.getParameter("page"));
        pageSize = NumberUtils.toInt(request.getParameter("pageSize"));

        if (StringUtils.isBlank(sortBy))
            sortBy = "ctime";

        if (StringUtils.isBlank(sortMethod))
            sortMethod = "desc";

        queryOrder.setSortBy(sortBy);
        queryOrder.setSortMethod(sortMethod);
        queryOrder.setStoreId(storeId);
        queryOrder.setPage(page);
        queryOrder.setPageSize(pageSize);

        ResponseMsg msg = new ResponseMsg();

        if (status == 1) {
            orderState.add(1);
        } else if (status == 2) {
            orderState.add(2);
            orderState.add(3);
        } else if (status == 0) {
            orderState.add(1);
            orderState.add(2);
            orderState.add(3);
        } else {
            msg.setMsg("暂无数据");
            return msg;
        }

        queryOrder.setStatus(orderState);

        QueryResult result = orderService.queryOrder(queryOrder);

        msg.setMsg("ok");
        msg.setData(result);
        msg.setCode(0);

        return msg;
    }

}
